Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | /** * Error Detail API * * GET /api/admin/monitoring/errors/[fingerprint] * Returns detailed error information including comments and history. * * PATCH /api/admin/monitoring/errors/[fingerprint] * Updates error status, assignment, priority, snooze settings. */ export const dynamic = 'force-dynamic'; import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { withAdmin, withErrorHandling, successResponse, errorResponse, type ApiSuccessResponse, type ApiErrorResponse, type AuthenticatedUser, } from '@/lib/api'; import type { ErrorStatistic, ErrorComment, ErrorHistory, ErrorLog, ErrorPriority, ErrorAction } from '@prisma/client'; import type { Session } from 'next-auth'; /** * Error detail response type */ interface ErrorDetailResponse { error: ErrorStatistic & { comments: ErrorComment[]; history: ErrorHistory[]; }; recentLogs: Pick<ErrorLog, 'id' | 'level' | 'message' | 'stack' | 'url' | 'metadata' | 'createdAt'>[]; } /** * Update error request body */ interface UpdateErrorBody { status?: string; assignedTo?: number | null; priority?: ErrorPriority; snoozedUntil?: string | null; ignoredReason?: string | null; } /** * GET handler - Get error details with comments and history */ async function handleGet( _request: NextRequest, context: { params?: Promise<Record<string, string>> } | undefined, // eslint-disable-next-line @typescript-eslint/no-unused-vars _session: Session, // eslint-disable-next-line @typescript-eslint/no-unused-vars _user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<ErrorDetailResponse> | ApiErrorResponse>> { const params = await context?.params; const fingerprint = params?.fingerprint; if (!fingerprint) { return errorResponse('INVALID_PARAM', 'Fingerprint is required', { status: 400 }); } const error = await prisma.errorStatistic.findUnique({ where: { fingerprint }, include: { comments: { orderBy: { createdAt: 'desc' }, take: 50, }, history: { orderBy: { createdAt: 'desc' }, take: 100, }, }, }); if (!error) { return errorResponse('NOT_FOUND', 'Error not found', { status: 404 }); } // Get recent error log occurrences const recentLogs = await prisma.errorLog.findMany({ where: { fingerprint }, orderBy: { createdAt: 'desc' }, take: 20, select: { id: true, level: true, message: true, stack: true, url: true, metadata: true, createdAt: true, }, }); return successResponse({ error, recentLogs }); } /** * PATCH handler - Update error status, assignment, priority, etc. */ async function handlePatch( request: NextRequest, context: { params?: Promise<Record<string, string>> } | undefined, _session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<{ error: ErrorStatistic }> | ApiErrorResponse>> { const params = await context?.params; const fingerprint = params?.fingerprint; if (!fingerprint) { return errorResponse('INVALID_PARAM', 'Fingerprint is required', { status: 400 }); } const body: UpdateErrorBody = await request.json(); const { status, assignedTo, priority, snoozedUntil, ignoredReason } = body; // Find existing error const current = await prisma.errorStatistic.findUnique({ where: { fingerprint }, }); if (!current) { return errorResponse('NOT_FOUND', 'Error not found', { status: 404 }); } // Build update data and history entries const updates: Record<string, unknown> = {}; const historyEntries: Array<{ errorStatId: number; userId: number; userEmail: string; action: ErrorAction; previousValue?: string | null; newValue?: string | null; }> = []; const userId = user.id; const userEmail = user.email; // Handle status change if (status && status !== current.status) { updates.status = status; if (status === 'resolved') { updates.resolvedAt = new Date(); updates.resolvedBy = userId; } else if (status === 'open' && current.status === 'resolved') { updates.resolvedAt = null; updates.resolvedBy = null; } historyEntries.push({ errorStatId: current.id, userId, userEmail, action: status === 'open' && current.status !== 'open' ? 'REOPENED' : 'STATUS_CHANGED', previousValue: current.status, newValue: status, }); } // Handle assignment change if (assignedTo !== undefined && assignedTo !== current.assignedTo) { updates.assignedTo = assignedTo; updates.assignedAt = assignedTo ? new Date() : null; historyEntries.push({ errorStatId: current.id, userId, userEmail, action: assignedTo ? 'ASSIGNED' : 'UNASSIGNED', previousValue: current.assignedTo?.toString() || null, newValue: assignedTo?.toString() || null, }); } // Handle priority change if (priority && priority !== current.priority) { updates.priority = priority; historyEntries.push({ errorStatId: current.id, userId, userEmail, action: 'PRIORITY_CHANGED', previousValue: current.priority, newValue: priority, }); } // Handle snooze change if (snoozedUntil !== undefined) { const newSnoozedUntil = snoozedUntil ? new Date(snoozedUntil) : null; const currentSnoozed = current.snoozedUntil?.toISOString() || null; const newSnoozed = newSnoozedUntil?.toISOString() || null; if (currentSnoozed !== newSnoozed) { updates.snoozedUntil = newSnoozedUntil; if (snoozedUntil) { updates.status = 'snoozed'; } else if (current.status === 'snoozed') { updates.status = 'open'; } historyEntries.push({ errorStatId: current.id, userId, userEmail, action: snoozedUntil ? 'SNOOZED' : 'UNSNOOZED', previousValue: currentSnoozed, newValue: newSnoozed, }); } } // Handle ignore reason if (ignoredReason !== undefined && ignoredReason !== current.ignoredReason) { updates.ignoredReason = ignoredReason; if (ignoredReason && current.status !== 'ignored') { updates.status = 'ignored'; historyEntries.push({ errorStatId: current.id, userId, userEmail, action: 'IGNORED', previousValue: current.status, newValue: 'ignored', }); } } // If no updates, return current error if (Object.keys(updates).length === 0) { return successResponse({ error: current }); } // Update in transaction const [updated] = await prisma.$transaction([ prisma.errorStatistic.update({ where: { fingerprint }, data: updates, }), ...(historyEntries.length > 0 ? [prisma.errorHistory.createMany({ data: historyEntries })] : []), ]); return successResponse({ error: updated }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const PATCH = withErrorHandling(withAdmin(handlePatch)); |